/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.sensor;

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.LayerAction;
import org.jdesktop.swingx.JXPanel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.util.Utilities;

/**
 *
 * @author Timon Veenstra
 */
public class ChangePaletteAction extends LayerAction {

    private Dialog dlg;
    private Layer layer;
    private static final List<SensorPalette> PALETTES = new ArrayList<SensorPalette>();
    private final JComboBox comboBox = new JComboBox(PALETTES.toArray());

    static {
        PALETTES.add(SensorPalette.PALLET01_04);
        PALETTES.add(SensorPalette.PALLET00_10);
        PALETTES.add(SensorPalette.PALLET01_06);
        PALETTES.add(SensorPalette.PALLET01_08);
        PALETTES.add(SensorPalette.PALLET02_04);
        PALETTES.add(SensorPalette.PALLET02_06);
        PALETTES.add(SensorPalette.PALLET02_08);
    }

    public ChangePaletteAction() {
        putValue(NAME, "change palette");
        comboBox.setEditable(false);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        layer = Utilities.actionsGlobalContext().lookup(Layer.class);
        assert layer != null;

        JXPanel panel = new JXPanel(new MigLayout("wrap 2"));

        comboBox.setSelectedItem(layer.getPalette());
        panel.add(new JLabel("choose:"));
        panel.add(comboBox);

        DialogDescriptor dd = new DialogDescriptor(panel, "change palette");
        dd.setOptions(new Object[]{getOkButton(), getCancelButton()});
        dlg = DialogDisplayer.getDefault().createDialog(dd);
        dlg.setVisible(true);
    }

    private JButton getOkButton() {
        JButton jButtonOK = new JButton();
        jButtonOK.setText("Ok");
        jButtonOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
                if (comboBox.getSelectedItem() != null) {
                    layer.setPalette((SensorPalette) comboBox.getSelectedItem());
                }
                
            }
        });
        return jButtonOK;
    }

    private JButton getCancelButton() {
        JButton jButtonCancel = new JButton();
        jButtonCancel.setText("cancel");
        jButtonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                dlg.setVisible(false);
            }
        });
        return jButtonCancel;
    }
}
